define(['jquery', 'bootstrap', 'backend', 'table', 'form'], function ($, undefined, Backend, Table, Form) {

    var Controller = {
        index: function () {
            // 初始化表格参数配置
            Table.api.init({
                extend: {
                    index_url: 'goods/goods/index' + location.search,
                    add_url: 'goods/goods/add',
                    batchedit_url: 'goods/goods/batchedit',
                    edit_url: 'goods/goods/edit',
                    del_url: 'goods/goods/del',
                    multi_url: 'goods/goods/multi',
                    import_url: 'goods/goods/import',
                    table: 'goods',
                }
            });

            var table = $("#table");

            // 初始化表格
            table.bootstrapTable({
                url: $.fn.bootstrapTable.defaults.extend.index_url,
                pk: 'id',
                sortName: 'id',
                fixedColumns: true,
                fixedRightNumber: 1,
                columns: [
                    [
                        {checkbox: true},
                        {field: 'id', title: __('Id'),operate:false},
                        {field: 'title', title: __('Title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'short_title', title: __('Short_title'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'goods_no', title:'唯一编号', operate: false},
                        {field: 'image', title: __('Image'), operate: false, events: Table.api.events.image, formatter: Table.api.formatter.image},
                        {field: 'goods_category_id', title: __('Goods_category_id'),formatter:function(value,row){
                            return row.goods_category.map(item=>item.title).join(',');
                        },searchList:$.getJSON('goods/goods_category/getJson')},
                        {field: 'biaoben_type_id', title: __('Biaoben_type_id'),formatter:function(value,row){
                            return row.biaoben_type?.title;
                        },searchList:$.getJSON('goods/biaoben_type/getJson')},
                        // {field: 'biaoben_rongqi.title', title: __('Biaoben_rongqi_id')},
                        // {field: 'num', title: __('Num')},
                        {field: 'keshi', title: __('Keshi'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        // {field: 'fangfa', title: __('Fangfa'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'time', title: __('Time'),operate: false,  table: table, class: 'autocontent'},
                        {field: 'price', title: __('Price'), operate: false},
                        {field: 'original_price', title: __('Original_price'), operate: false},
                        {field: 'waijian_tags', title: __('Waijian_tags'), operate: 'LIKE', formatter: Table.api.formatter.flag},
                        {field: 'hospital', title: __('Hospital'), operate: 'LIKE', table: table, class: 'autocontent', formatter: Table.api.formatter.content},
                        {field: 'createtime', title: __('Createtime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        // {field: 'updatetime', title: __('Updatetime'), operate:'RANGE', addclass:'datetimerange', autocomplete:false, formatter: Table.api.formatter.datetime},
                        {field: 'operate', title: __('Operate'), table: table, events: Table.api.events.operate, formatter: Table.api.formatter.operate}
                    ]
                ],
                exportOptions: {
                    onCellHtmlData:function($cell, row, col, htmlData){
                        console.log($cell, row, col, htmlData);
                        
                        return htmlData;
                    }
                },
            });

            $(document).on("click", ".btn-batchedit", function () {
                Backend.api.open('goods/goods/batchedit', '批量修改图片');
            })

            // 为表格绑定事件
            Table.api.bindevent(table);
        },
        add: function () {
            Controller.api.bindevent();
            $(document).on("change", "#c-commission_type", function(){
                //变更后的回调事件
                var commission_type = $(this).val();
                if(commission_type == 10){
                    $("#c-commission_price").val("0");
                    $("#c-commission_price").attr("disabled", true);
                }else{
                    $("#c-commission_price").attr("disabled", false);
                }
            });
        },
        edit: function () {
            Controller.api.bindevent();
            $(document).on("change", "#c-commission_type", function(){
                //变更后的回调事件
                var commission_type = $(this).val();
                if(commission_type == 10){
                    $("#c-commission_price").val("0");
                    $("#c-commission_price").attr("disabled", true);
                }else{
                    $("#c-commission_price").attr("disabled", false);
                }
            });
        },
        batchedit:function(){
            Controller.api.bindevent();
        },
        api: {
            bindevent: function () {
                Form.api.bindevent($("form[role=form]"));
            }
        }
    };
    return Controller;
});
